package com.yz.study.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class MethodExporterAspect
{

    @Around("@annotation(com.yz.study.annotations.MethodExporter)")
    public Object methodExporter(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {

        Object retValue = null;

        long startTime = System.currentTimeMillis();
        System.out.println("-----@Around环绕通知AAA-methodExporter-----上半联");
        retValue = proceedingJoinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long costTime = endTime - startTime;

        ObjectMapper objectMapper = new ObjectMapper();
        //1 将入参数Json序列化
        String jsonInputParam = objectMapper.writeValueAsString(proceedingJoinPoint.getArgs());
        //2 将返回结果retValue序列化
        String jsonResult = null;
        if(retValue != null){
            jsonResult = objectMapper.writeValueAsString(retValue);
        }else{
            jsonResult = "null";
        }

        log.info("\n方法分析上报中 " +
                "\n类名方法名:"+proceedingJoinPoint.getTarget().getClass().getName()+"."+proceedingJoinPoint.getSignature().getName()+"()"+
                "\n执行耗时:"+costTime+"毫秒"+
                "\n输入参数:"+jsonInputParam+""+
                "\n返回结果:"+jsonResult+"" +
                "\nover"
        );

        System.out.println("-----@Around环绕通知BBB-methodExporter-----下半联");
        return retValue;
    }


/*    @Around("execution(public void com.yz.study.pay.PayServiceImplV2.payV2*(..))")
    public Object aroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        Object retValue = null;

        System.out.println("5-----@Around环绕通知AAA---上半联");
        retValue = proceedingJoinPoint.proceed();
        System.out.println("6-----@Around环绕通知BBB---下半联");

        return retValue;
    }*/
}
